package com.infore.airsupervise.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.StationExpandE;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaotao on 2017/8/11.
 */

public class StationExpandAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity,BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */

    public static final int NO_SELECTE = 0;
    public static final int RB_SELECTE = 1 ;
    public static final int CB_SELECTE = 2 ;

    private int curSelIndex = -1; //存放当前单选 索引
    private int lastSelIndex  = -1; //存放上一次单选的索引
    private MultiItemEntity curItem = null ;   //当前单选对象

    private boolean selectorEnable = false; //是否显示选择框 ，默认不显示
    public int curType; //当前item类型
    private List<String> mnList  = null;

    private OnItemClickListener itemClickListener= null;

    public interface OnItemClickListener{
        void onClick(int itemType , MultiItemEntity item );
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }


    public StationExpandAdapter(List<MultiItemEntity> data ) {
        super(data);
        addItemType(0, R.layout.item_station_tree_f); //第一级
        addItemType(1,R.layout.item_station_tree_s);//第二级
        addItemType(2,R.layout.item_station_tree_t);//第三级

    }


    public void setSelectedEnable(boolean enable){
        selectorEnable = enable;
    }
    private void setCheckBoxEnable(CheckBox selector){
        //是否可选
        if(selectorEnable){
            selector.setVisibility(View.VISIBLE);
        }else {
            selector.setVisibility(View.GONE);
        }
    }
    /**
     * 设置选择功能
     * @param curType
     */
    public void setCurType(int curType) {
        this.curType = curType;
    }


    /**
     * 遍历数据集合 获取点选的选项
     * @return
     */
    public List<String> getSelectorPosition(){
        if(mnList == null){
            mnList = new ArrayList<>();
        }
        mnList.clear();
        for(MultiItemEntity item : getData()){
            switch (item.getItemType()){
                case 0 :
                    StationExpandE stationE = (StationExpandE) item;
                    if(stationE.isSelected()){
                        mnList.add(stationE.getMn());
                    }
                break;
                case 1 :
                    StationExpandE.Station2 stationS = (StationExpandE.Station2) item;
                    if(stationS.isSelected()) {
                        mnList.add(stationS.getMn());
                    }
                break;
                case 2 :
                    StationExpandE.Station3 stationT = (StationExpandE.Station3) item;
                    if(stationT.isSelected()){
                        mnList.add(stationT.getMn());
                    }
                break;
            }
        }
       return mnList;
    }

    /**
     * 获取单选数据
     * @return
     */
    public MultiItemEntity getSelectorItem(){
        return curItem;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
        final int pos = helper.getLayoutPosition();
        CheckBox selector = helper.getView(R.id.node_selector);
         RadioButton radioButton = helper.getView(R.id.rb_selector);
        final FrameLayout layoutSelecte = helper.getView(R.id.layout_selector);
        switch (curType){
            case NO_SELECTE : //跳转类型
                layoutSelecte.setVisibility(View.GONE);
                //可点击状态时设置点击事件
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(itemClickListener != null){
                            itemClickListener.onClick(item.getItemType() ,item);
                        }
                    }
                });
                break;
            case RB_SELECTE :
                selector.setVisibility(View.GONE);
                radioButton.setOnCheckedChangeListener(null);
                if(pos == curSelIndex){
                    radioButton.setChecked(true);
                }else {
                    radioButton.setChecked(false);
                }
                break;
            case CB_SELECTE :
                radioButton.setVisibility(View.GONE);
                selector.setOnCheckedChangeListener(null); //将checkbox的监听事件置空 防止复用item布局时重复监听
                break;
        }

        switch (item.getItemType()){
           case  0 :
               final StationExpandE stationF = (StationExpandE) item;

               helper.setText(R.id.node_value,stationF.getName());
               //展开收起
               final ImageView ivExpand = helper.getView(R.id.arrow_icon);
               if(stationF.hasSubItem()){
                   ivExpand.setVisibility(View.VISIBLE);
                   ivExpand.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {

                           if (stationF.isExpanded()){
                               collapse(pos);
                           }else {
                               expand(pos);
                           }
                       }
                   });
                   if(stationF.isExpanded()){
                       ivExpand.setImageResource(R.drawable.chevron_down);
                   }else {
                       ivExpand.setImageResource(R.drawable.chevron_right);
                   }
               }else {
                   ivExpand.setVisibility(View.INVISIBLE);
               }
               if(curType == CB_SELECTE){ //多选
                   selector.setChecked(stationF.isSelected()); //根据数据对象中的点选状态字段设置checkbox的状态
                   //设置监听事件
                   selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           //将点击状态保存至对应数据对象中
                           stationF.setSelected(isChecked);
                       }
                   });
               }
               if(curType == RB_SELECTE){ //单选
                   radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           if(isChecked) {
                               lastSelIndex = curSelIndex;
                               curSelIndex = pos;
                               if (lastSelIndex != -1) {
                                   notifyItemChanged(lastSelIndex);
                               }
                               curItem = item ;
                           }
                       }
                   });
               }
               break;
           case 1 :
               final StationExpandE.Station2 stationS = (StationExpandE.Station2) item;

               helper.setText(R.id.node_value,stationS.getName());

               final ImageView ivExpandS = helper.getView(R.id.arrow_icon);
               LinearLayout layout_wrapper = helper.getView(R.id.picture_wrapper);
              //先判断是否可以展开
               if(stationS.hasSubItem()){
                   layout_wrapper.setVisibility(View.GONE);
                   ivExpandS.setVisibility(View.VISIBLE);
                   ivExpandS.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {

                           if (stationS.isExpanded()){
                               collapse(pos);
                               ivExpandS.setImageResource(R.drawable.chevron_right);
                           }else {
                               expand(pos);
                               ivExpandS.setImageResource(R.drawable.chevron_down);
                           }
                       }
                   });
                   if(stationS.isExpanded()){
                       ivExpandS.setImageResource(R.drawable.chevron_down);
                   }else {
                       ivExpandS.setImageResource(R.drawable.chevron_right);
                   }
               }else {
                   ivExpandS.setVisibility(View.GONE);
                   layout_wrapper.setVisibility(View.VISIBLE);
               }
               if(curType == CB_SELECTE){
                   selector.setChecked(stationS.isSelected());
                   selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           stationS.setSelected(isChecked);
                       }
                   });
               }
               if(curType == RB_SELECTE){ //单选
                   radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           if(isChecked) {
                               lastSelIndex = curSelIndex;
                               curSelIndex = pos;
                               if (lastSelIndex != -1) {
                                   notifyItemChanged(lastSelIndex);
                               }
                               curItem = item ;
                           }
                       }
                   });
               }
               break;
           case 2 :
               final StationExpandE.Station3 stationT = (StationExpandE.Station3) item;
               helper.setText(R.id.node_value,stationT.getName());
               if(curType == CB_SELECTE){
                   selector.setChecked(stationT.isSelected());
                   selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           stationT.setSelected(isChecked);
                       }
                   });
               }
               if(curType == RB_SELECTE){ //单选
                   radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                           if(isChecked) {
                               lastSelIndex = curSelIndex;
                               curSelIndex = pos;
                               if (lastSelIndex != -1) {
                                   notifyItemChanged(lastSelIndex);
                               }
                           }
                           curItem = item ;
                       }
                   });
               }
               break;
       }

    }
}
